package com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.persenter;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.adapter.FileSetConfigAdapter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.base.BaseAdapter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.bean.DevicesCustomConfigurationBean;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.config.RBConfig;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.inter.IonClickedItemListenter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.contract.IFileSetConfigContract;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.modle.FileSetConfigModle;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.JsonUitl;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.RecyclerViewUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/8/14.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class FileSetConfigPersenter implements IFileSetConfigContract.IFileSetConfigPersenter {
    private IFileSetConfigContract.IFileSetConfigView mIFileSetConfigView;
    private FileSetConfigAdapter fileSetConfigAdapter;

    private IFileSetConfigContract.IFileSetConfigModle mFileSetConfigModle;
    private String TAG = FileSetConfigPersenter.class.getSimpleName();
    private Handler handler = new Handler();
    private RecyclerView tRecyclerView;

    private List<DevicesCustomConfigurationBean> mlist;
    private List<DevicesCustomConfigurationBean.DeviceMousesBean> beanlist;
    private DevicesCustomConfigurationBean mbean;

    public FileSetConfigPersenter(IFileSetConfigContract.IFileSetConfigView mIFileSetConfigView) {
        this.mIFileSetConfigView = mIFileSetConfigView;
        mFileSetConfigModle = new FileSetConfigModle();
    }

    @Override
    public void initTitle() {
        mIFileSetConfigView.showTitle();
    }

    @Override
    public void initRecyclerView(RecyclerView mRecyclerView, Context mContext) {
        tRecyclerView = mRecyclerView;
        RecyclerViewUtils mRecyclerViewUtils =   new RecyclerViewUtils();
        fileSetConfigAdapter = new FileSetConfigAdapter(mContext);
        mRecyclerViewUtils.initView(mRecyclerView,0,mContext, fileSetConfigAdapter);
        setDevicesManagerAdapterListener(fileSetConfigAdapter,mContext);
    }





    @Override
    public void loadDate(final Context mContext, String id, final int postion) {
        OkHttpUtils
                .post()
                .url(RBConfig.HOST_IP+RBConfig.GET_DEVICES_ATTRIB)
                .addHeader("sessionId",mFileSetConfigModle.getToken(mContext))
                .addParams("deviceId", id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        // Log.e(TAG, "loadRecyclerViewDate:token ==sdg111===> "+ response.body().string());
                        String NetWorkResponse = JsonUitl.NetWorkResponse(response);
                        if(NetWorkResponse != null){
                            JSONObject object = JsonUitl.NetworkResponseDateDeal(NetWorkResponse);
                            if(object != null){
                                int codo = JsonUitl.getParsinCode(object);
                                if(codo == 100){
                                    String normalDate = JsonUitl.getStringDateFeld(object);
                                    Type type = new TypeToken<List<DevicesCustomConfigurationBean>>(){}.getType();
                                    mlist = new Gson().fromJson(normalDate,type);
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            mbean = mlist.get(postion);
                                            if(mbean != null){
                                                beanlist = mbean.getDeviceMouses();
                                                fileSetConfigAdapter.AddItemDate(beanlist);
                                                initDate(mContext,postion);
                                            }
                                           ;
                                        }
                                    });
                                }
                            }
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    @Override
    public void initDate(Context mContext, int postion) {
        if(mbean != null ){
            String name =  mbean.getName();
            if(name != null && !TextUtils.isEmpty(name)){
                mIFileSetConfigView.SetFileName(name);
            }
            String mettingName =   mbean.getMeetName();
            if(mettingName != null && !TextUtils.isEmpty(mettingName)){
                mIFileSetConfigView.setMettingName(mettingName);
            }
        }

    }

    @Override
    public void SaveDate(Context mContext,int postion) {
        if(mbean != null){
            String name = mbean.getName();
            String mettingName = mbean.getMeetName();
           String id =  mbean.getId();
           String deviceid =  mbean.getDeviceId();
            if(name != null &&!TextUtils.isEmpty(name) ){
                if(mettingName != null && !TextUtils.isEmpty(mettingName)){
                    if(id != null &&!TextUtils.isEmpty(id) ){
                        if(deviceid != null && !TextUtils.isEmpty(deviceid)){
                            submitDate(name,mettingName,id,deviceid,mContext);
                        }
                    }
                }
            }
        }
    }
    /** @desc 保存数据
     * @param name          配置文件名称
     * @param mettingName       会议名称
     * @param id                id
     * @param deviceid          设备id
     * @param mContext          上下文
     * */
    private void submitDate(String name, String mettingName, String id, String deviceid, Context mContext) {
        OkHttpUtils
                .post()
                .url(RBConfig.HOST_IP+RBConfig.SAVE_DEVICS_CONFIG_INFO_FILE)
                .addHeader("sessionId",mFileSetConfigModle.getToken(mContext))
                .addParams("deviceId", deviceid)
                .addParams("meetName", mettingName)
                .addParams("name", name)
                .addParams("id", id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                       // Log.e(TAG, "loadRecyclerViewDate:token =====> "+ response.body().string());
                        String NetWorkResponse = JsonUitl.NetWorkResponse(response);
                        if(NetWorkResponse != null){
                            JSONObject object = JsonUitl.NetworkResponseDateDeal(NetWorkResponse);
                            if(object != null){
                                int codo = JsonUitl.getParsinCode(object);
                                final String normalDate = JsonUitl.getStringDateFeld(object);
                                if(codo == 100) {
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            mIFileSetConfigView.finshPager();
                                        }
                                    });
                                }
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        mIFileSetConfigView.showToastMessage(normalDate);
                                    }
                                });
                            }
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                mIFileSetConfigView.showToastMessage("保存设备配置失败！");
                            }
                        });
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });

    }


    /**
     * @desc 设置监听
     * */
    private void setDevicesManagerAdapterListener(BaseAdapter managerAdapter, final Context mContext) {
        managerAdapter.setmIonClickedItemListenter(new IonClickedItemListenter() {
            @Override
            public void onClickedItem(View view) {
              int postion =   tRecyclerView.getChildLayoutPosition(view);
                DevicesCustomConfigurationBean.DeviceMousesBean bean =   getDevicesMouse(postion);
                mIFileSetConfigView.toMouseSetPager(bean,postion,mbean.getId());
            }

            @Override
            public void onClickedFist(int postion) {

            }

            @Override
            public void onClickedSecond(int postion) {

            }
        });
    }
    /**
     * @desc 获取鼠标设置信息
     * */
    private DevicesCustomConfigurationBean.DeviceMousesBean getDevicesMouse(int postion){
        for (DevicesCustomConfigurationBean.DeviceMousesBean deviceMousesBean : beanlist) {
            int mouseNume = deviceMousesBean.getMouseNum();
            if(mouseNume == (postion+1)){
                return deviceMousesBean;
            }
        }
        return null;
    }
}
